package wifiunion.com.notice.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.DatePicker;


import java.util.Calendar;
import java.util.Date;

import wifiunion.com.notice.R;
import wifiunion.com.notice.utils.TimeUtil;


public class MyDateTimePickerDialog extends Dialog {
    /**
     * 取消按钮
     */
    private View mCancleButton;
    /**
     * 确定按钮
     */
    private View mOKButton;
    /**
     * 日期控件
     */
    private DatePicker mDP;
  /*  *//**
     * 时间控件
     *//*
    private TimePicker mTP;*/
    private View.OnClickListener defaultListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            cancel();
        }
    };

    public MyDateTimePickerDialog(Context context) {
        super(context, R.style.alert_dialog);
        LayoutInflater li = LayoutInflater.from(context);
        ViewGroup contentView = (ViewGroup) li.inflate(
                R.layout.date_time_picker, null);
        mCancleButton = contentView.findViewById(R.id.cancel_btn);
        mOKButton = contentView.findViewById(R.id.ok_btn);
        mDP = (DatePicker) contentView.findViewById(R.id.date_picker);
//        mTP = (TimePicker) contentView.findViewById(R.id.time_picker);
//        mTP.setIs24HourView(true);
        mCancleButton.setOnClickListener(defaultListener);
        setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        onWindowAttributesChanged(lp);
        setContentView(contentView);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        getWindow().setAttributes(lp);
    }

    public void setOkBtnListener(View.OnClickListener listener) {
        if (mOKButton != null) {
            mOKButton.setOnClickListener(listener);
        }
    }

    public void setOkBtnTag(Object tag) {
        if (mOKButton != null) {
            mOKButton.setTag(tag);
        }
    }

    public void setDefaultDateTime(Date date) {

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        mDP.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
//        mTP.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
//        mTP.setCurrentMinute(c.get(Calendar.MINUTE));
    }

    public void setMaxAndMinValue(long maxDate, long minDate) {
        mDP.setMaxDate(maxDate);
        mDP.setMinDate(minDate);
    }

    public String getDateTimeString() {
        Calendar c = Calendar.getInstance();
        c.set(mDP.getYear(), mDP.getMonth(), mDP.getDayOfMonth(), 0, 0, 0);
        return TimeUtil.getDateString(c.getTime());
    }

    public String getYMDDateTimeString() {
        Calendar c = Calendar.getInstance();
        c.set(mDP.getYear(), mDP.getMonth(), mDP.getDayOfMonth(), 0, 0, 0);
        return TimeUtil.getYMDString(c.getTime());
    }
}
